export const notEmpty = (val) => {
  return ![null, undefined, ''].includes(val)
}

export const isValRepeat = (list: any[], val: any, key: string) => {
  try {
    let hasRepeat = false
    const map = {}

    for (const item of list) {
      const keyId = item[key]
      if (val === keyId) {
        if (!map[keyId]) {
          map[keyId] = 1
        } else {
          hasRepeat = true
          break
        }
      }
    }
    return hasRepeat
  } catch (e) {
    console.log(e, '报错了')
  }
}

export const getRepeatMap = (
  list: any[],
  keyMap?: {
    parentKey
    sonKey
  }
) => {
  try {
    let repeatFlag = false
    const { parentKey = 'productId', sonKey = 'binId' } = keyMap || {}
    const repeatMap = {}
    for (const item of list) {
      const parentId = item[parentKey]
      const sonId = item[sonKey]

      if (!repeatMap[parentId]) {
        repeatMap[parentId] = {
          [sonId]: 1
        }
      } else {
        if (repeatMap[parentId][sonId]) {
          repeatFlag = true
          repeatMap[parentId][sonId] += 1
        } else {
          repeatMap[parentId][sonId] = 1
        }
      }
    }
    return repeatMap
  } catch (e) {
    console.log(e, '报错了')
  }
}

// 查找数组中是否有不同prop属性
export const isDifferentProp = (list: any[], prop: string) => {
  // prop-supplierId
  const saveList: number[] = []
  let flag = false
  for (let i = 0; i < list.length; i++) {
    const propId = list[i][prop]
    if (!saveList.includes(propId)) {
      saveList.push(propId)
      // 说明有不同的
      if (saveList.length > 1) {
        flag = true
        break
      }
    }
  }
  return flag
}

export const getSameMap = (sameStrList: any[], list: any) => {
  const createSameStr = (sameList: any[], item: any) => {
    let str = ''
    sameList.forEach((same) => {
      str += (item[same] || '') + '—'
    })
    return str
  }

  unref(list).forEach((item) => {
    item.sameStr = createSameStr(sameStrList, item)
  })

  const sameMap: any = {}
  unref(list).forEach((item) => {
    if (!sameMap[item.sameStr]) {
      sameMap[item.sameStr] = 1
    } else {
      sameMap[item.sameStr] += 1
    }
  })

  return {
    createSameStr,
    sameMap
  }
}

export const useInitNum = () => {
  let initNum = 0
  const addInitNum = () => {
    nextTick(() => {
      initNum++
    })
  }
  const judgeNum = () => {
    return initNum < 1
  }
  return {
    addInitNum,
    judgeNum
  }
}

export const judgeType = (type, val) => {
  const typeMap = {
    '[object Undefined]': 'undefined',
    '[object Null]': 'null',
    '[object Number]': 'number',
    '[object Boolean]': 'boolean',
    '[object String]': 'string',
    '[object Symbol]': 'symbol',
    '[object Object]': 'object',
    '[object Array]': 'array',

    '[object BigInt]': 'bigint',
    '[object Function]': 'function',
    '[object Date]': 'date',
    '[object RegExp]': 'regexp',
    '[object Error]': 'error',
    '[object Arguments]': 'arguments',
    '[object Set]': 'set',
    '[object Map]': 'map',
    '[object WeakSet]': 'weakset',
    '[object WeakMap]': 'weakmap'
  }

  const result = Object.prototype.toString.call(val)
  return typeMap[result] === type
}

// 判断基本数据类型数组-判断这个值是不是基础数据类型
export const judgeBaseTypeList = (val) => {
  const typeList = ['number', 'string', 'boolean', 'undefined', 'null']
  const flag = typeList.some((item) => judgeType(item, val))
  return flag
}